'use strict';

function loadAndInitWA(waURL) {
    if (!WebAssembly.instantiateStreaming) { // polyfill
        WebAssembly.instantiateStreaming = async (resp, importObject) => {
            const source = await (await resp).arrayBuffer();
            return await WebAssembly.instantiate(source, importObject);
        };
    }
    const go = new Go();
    WebAssembly.instantiateStreaming(fetch(waURL), go.importObject)
    .then(async (result) => {
        await go.run(result.instance);
    });
}

function post(url, data) {
    return fetch(url, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(data),
    })
}
// get = fetch
(() => {

    const fingerprint = async () => {
        while (typeof global.fingerprint !== "function") {
            await new Promise(resolve => requestAnimationFrame(resolve))
        }
        return global.fingerprint();
    };

    fingerprint().then((info) => {
        let box = document.querySelector("#fingerprint");
        post("//api.e4ting.cn/api/v2/bot/client", info)
        .then(response => response.json())
        .then( (ret) => {
            console.log(ret)
            box.innerHTML = info["uuid"] + " - " + ret.data;
        } )
    });
})();